package spectra.ee.web.mileage.service;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.map.CaseInsensitiveMap;

import com.ibatis.sqlmap.client.SqlMapClient;

import spectra.base.BasePublic;
import spectra.base.licensemanager.Encrypt;
import spectra.base.log.Logger;
import spectra.base.sql.ibatis.SqlMapperWeb;
import spectra.base.util.DateUtil;
import spectra.base.util.FileUtil;
import spectra.base.util.StringUtil;
import spectra.ee.EEWebConfig;
import spectra.ee.EEPublic;
import spectra.ee.web.WebConfig;
import spectra.ee.web.WebPublic;
import spectra.ee.web.mileage.model.MileageDetail;
import spectra.ee.web.mileage.model.MileageScore;

/**
 * 마일리지 관련 클래스.
 *
 * @author kmhan
 */
public class MileageServiceBase
{

    /** Logger객체. */
    private static Logger logger = Logger.getLoggerInstance(MileageServiceBase.class.getName());

    /** SqlMapClient 객체. */
    protected static SqlMapClient sqlMap = SqlMapperWeb.getSqlMapWebInstance();

    
    /**
     * 해당 기간의 
     * @param domainId
     * @param currDate
     * @param mileageType
     * @return
     */
    public MileageDetail selectMileageDetailByMileageType(String domainId, String currDate, String mileageType)
    {
    	MileageDetail mileageDetail = null;
        try
        {
        	MileageDetail obj = new MileageDetail();
        	obj.setDomainId(domainId);
        	obj.setMileageType(mileageType);
        	obj.setCurrDate(currDate);
 	 	
        	mileageDetail = (MileageDetail) sqlMap.queryForObject("Mileage.selectMileageDetailByMileageType", obj);
        }
        catch (SQLException e)
        {
            logger.error(e.getMessage());
        }
        return mileageDetail;
    }
    
    /**
     * 특정기간내의 누적되어 있는 지식 마일리지 점수를 가져오는 메소드이다.
     * @param domainId
     * @param accountId
     * @param startDate
     * @param endDate
     * @return
     */
    public List selectKbMileageScoreByAccountId(String domainId, String accountId, String startDate, String endDate)
    {
    	List list = null;
    	
    	try
        {
    		MileageScore obj = new MileageScore();
    		obj.setDomainId(domainId);
    		obj.setAccountId(accountId);
    		obj.setStartDate(startDate);
    		obj.setEndDate(endDate);
    		obj.setServiceType(WebPublic.SERVICE_TYPE_KB);
            list = (List)sqlMap.queryForList("Mileage.selectMileageScoreByAccountId", obj);
        }
        catch (SQLException e)
        {
            logger.error(e.getMessage());
        }
        return list;
    }
    
    /**
     * 마일리지 점수로 마일리지 등급에 설정되어 있는 이미지 URL을 가져온다. 
     * @param domainId
     * @param mileageScore
     * @return
     */
    public String selectKbMileageImageUrl(String domainId, int mileageScore)
    {
    	String result = "";
    	String attachDownloadUrl = WebConfig.getEnvValue(WebPublic.COM_ATTACH_DOWNLOAD_URL);    	
        
        try
        {        	
        	MileageScore obj = new MileageScore();
    		obj.setDomainId(domainId);
    		obj.setScore(mileageScore);
    		
            String photoFilename  = (String)sqlMap.queryForObject("Mileage.selectKbMileageImageUrl", obj);
            if (photoFilename == null)
            	return null;
            
        	result = attachDownloadUrl + "?isAttach=true";
        	String [] arrFileInfo = new String[2];
        	arrFileInfo[0] = WebPublic.KBHELPER_MILEAGE_ATTACH_PATH + "/" + photoFilename;
            arrFileInfo[1] = photoFilename;
            
            String strEncText = Encrypt.crypt(arrFileInfo, EEWebConfig.getEncryptKey(), BasePublic.ENCODING);            
            strEncText = java.net.URLEncoder.encode(strEncText);
            result = attachDownloadUrl + "?isAttach=true&fileInfo=" + strEncText;
            //System.out.println(result);
        }
        catch(Exception e)
        {
        }
        
    	return result;
    }
    
}
